package msb.class07;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Assert;
import org.junit.Test;

import java.util.Comparator;
import java.util.Random;

public class HeapTest {

    public static class TestObject {
        private int num;

        public TestObject() {
        }

        public TestObject(int num) {
            this.num = num;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }

    public static class TestObjectComparator implements Comparator<TestObject> {
        @Override
        public int compare(TestObject o1, TestObject o2) {
            return o1.num - o2.num;
        }
    }

    @Test
    public void testSort() {
        Random random = new Random();
        Heap<TestObject> heap = new Heap<>(new TestObjectComparator());
        for (int i = 0; i < 100; i++) {
            TestObject object = new TestObject(random.nextInt(100));
            System.out.println(object.num);
            heap.push(object);
        }
        System.out.println("-------");
        while (heap.peek() != null) {
            TestObject o = heap.pop();
            System.out.println(o.num);
        }
    }

    @Test
    public void testMap() {
        Random random = new Random();
        Heap<TestObject> heap = new Heap<>(new TestObjectComparator());
        for (int i = 0; i < 100; i++) {
            TestObject object = new TestObject(random.nextInt(100));
            heap.push(object);
        }
        for (int i = 0; i < heap.list.size(); i++) {
            TestObject testObject = heap.list.get(i);
            Assert.assertSame(i, heap.map.get(testObject));
        }
    }

}
